home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 January: Mac OS SDK / Dev.CD Jan 00 SDK1.toast / Development Kits / Mac OS / Display Manager SDK / Sample Code / Display Changed CWPro4 / Source / scrolls.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-20  |  6.9 KB  |  309 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2. #
  3. #        scrolls.c
  4. #
  5. #        This segment handles the scroll bars.
  6. #
  7. #        Author(s):     Michael Marinkovich
  8. #                    marink@apple.com
  9. #
  10. #        Modification History: 
  11. #
  12. #            1/19/99        ewa     update to CWPro3 and universal interfaces                     
  13. #            10/12/95    MWM        cleaned up
  14. #            6/4/95        MWM     Initial coding                     
  15. #
  16. #        Copyright © 1992-96 Apple Computer, Inc., All Rights Reserved
  17. #
  18. #
  19. #        You may incorporate this sample code into your applications without
  20. #        restriction, though the sample code has been provided "AS IS" and the
  21. #        responsibility for its operation is 100% yours.  However, what you are
  22. #        not permitted to do is to redistribute the source as "DSC Sample Code"
  23. #        after having made changes. If you're going to re-distribute the source,
  24. #        we require that you make it clear in the source that the code was
  25. #        descended from Apple Sample Code, but that you've made changes.
  26. #
  27. *************************************************************************************/
  28.  
  29. #include <Events.h>
  30. #include <ToolUtils.h>
  31. #include <Gestalt.h>
  32. #include <OSUtils.h>
  33.  
  34.  
  35. #include "App.h"
  36. #include "Proto.h"
  37.  
  38.  
  39. //----------------------------------------------------------------------
  40. //    Globals -
  41. //----------------------------------------------------------------------
  42.  
  43. extern Boolean        gInBackground;
  44.  
  45.  
  46. //----------------------------------------------------------------------
  47. //
  48. //    InstallScrollBars -
  49. //                 
  50. //
  51. //----------------------------------------------------------------------
  52.  
  53. void InstallScrollBars(WindowRef window, DocHnd doc)
  54. {
  55.     Rect        bounds;
  56.     Str255        str = "\p";
  57.     
  58.     // calc horiz scroll bar
  59.     bounds = window->portRect;
  60.     
  61.     bounds.right++;
  62.     bounds.left = bounds.right - (kScrollWidth + 1);
  63.     bounds.top--;
  64.     bounds.bottom -= (kScrollWidth - 1);
  65.     
  66.     (**doc).hScroll = NewControl(window, &bounds, str, true,
  67.                                  0, 0, 0, scrollBarProc, nil);
  68.                                   
  69.     // calc vert scroll bar
  70.     bounds = window->portRect;
  71.  
  72.     bounds.left--;
  73.     bounds.right -= (kScrollWidth - 1);
  74.     bounds.top = bounds.bottom - kScrollWidth;
  75.     bounds.bottom++;
  76.     
  77.  
  78.     (**doc).vScroll = NewControl(window, &bounds, str, true,
  79.                                  0, 0, 0, scrollBarProc, nil);
  80.  
  81.                                   
  82. }
  83.  
  84.  
  85. //----------------------------------------------------------------------
  86. //
  87. //    AdjustScrollValues -
  88. //                 
  89. //
  90. //----------------------------------------------------------------------
  91.  
  92. void AdjustScrollValues(WindowRef window)
  93. {
  94.     Rect                    contRect;
  95.     Rect                    picFrame;
  96.     ControlHandle            hCntrl;
  97.     ControlHandle            vCntrl;
  98.     short                    oldValue;
  99.     short                    lines;
  100.     short                    max;
  101.     DocHnd                    doc;
  102.     
  103.     doc = (DocHnd)GetWRefCon(window);
  104.     
  105.     if (doc != nil) 
  106.     {
  107.         hCntrl = (**doc).hScroll;
  108.         vCntrl = (**doc).vScroll;
  109.         
  110.         GetContRect(window,&contRect);
  111.         picFrame = (**doc).world->portRect;
  112.     
  113.         oldValue = GetControlValue(vCntrl);
  114.         lines = picFrame.bottom - picFrame.top;
  115.         max = lines - contRect.bottom ;
  116.         if ( max < 0 ) max = 0;
  117.         SetControlMaximum(vCntrl, max);
  118.     
  119.         if(oldValue + contRect.bottom > lines)
  120.             SetControlValue(vCntrl, max);
  121.         
  122.         oldValue = GetControlValue(hCntrl);
  123.         lines = picFrame.right - picFrame.left;
  124.         max = lines - contRect.right;
  125.         if ( max < 0 ) max = 0;
  126.         SetControlMaximum(hCntrl, max);
  127.     
  128.         if(oldValue + contRect.right > lines)
  129.             SetControlValue(hCntrl, max);
  130.     }        
  131.             
  132. }
  133.  
  134.  
  135. //----------------------------------------------------------------------
  136. //
  137. //    AdjustScrollbars -
  138. //                 
  139. //
  140. //----------------------------------------------------------------------
  141.  
  142. void AdjustScrollbars(WindowRef window, Boolean resize)
  143. {
  144.     ControlRef        hCtl;
  145.     ControlRef        vCtl;
  146.     Rect            r;
  147.     DocHnd             doc;
  148.  
  149.     
  150.     doc = (DocHnd)GetWRefCon(window);
  151.     
  152.     if (doc != nil && resize) 
  153.     {
  154.         hCtl = (**doc).hScroll;
  155.         vCtl = (**doc).vScroll;
  156.         GetContRect(window, &r);
  157.         
  158.         // resize horizontal
  159.         MoveControl(hCtl, -1, r.bottom);
  160.         SizeControl(hCtl, (r.right - r.left) + 2, kScrollWidth + 1);
  161.         
  162.         // resize vertical
  163.         MoveControl(vCtl, r.right, -1);
  164.         SizeControl(vCtl, kScrollWidth + 1, r.bottom + 2);
  165.     }
  166.     
  167.     // adjust scrollbar values to match
  168.     AdjustScrollValues(window);
  169. }
  170.  
  171.  
  172. //----------------------------------------------------------------------
  173. //
  174. //    ScrollActionProc -
  175. //                 
  176. //
  177. //----------------------------------------------------------------------
  178.  
  179. pascal void ScrollActionProc(ControlRef control, short part)
  180. {
  181.     WindowRef        window;
  182.     short            amount;
  183.     short            value;
  184.     short            max;
  185.     short            hAmount;
  186.     short            vAmount;
  187.     DocHnd            doc;
  188.     
  189.     window = (**control).contrlOwner;
  190.     doc = (DocHnd)GetWRefCon(window);
  191.     
  192.     if (doc != nil) 
  193.     {
  194.         if ( part != 0 ) 
  195.         {                
  196.             window = (*control)->contrlOwner;
  197.             hAmount = vAmount = 0;
  198.             
  199.             value = GetControlValue(control);    
  200.             max = GetControlMaximum(control);        
  201.         
  202.             switch ( part ) 
  203.             {
  204.                 case kControlUpButtonPart:
  205.                 case kControlDownButtonPart:        /* one line */
  206.                         amount = 8;
  207.                         break;
  208.                         
  209.                 case kControlPageUpPart:            /* one page */
  210.                 case kControlPageDownPart:
  211.                         amount = 64;
  212.                         break;
  213.             }
  214.             
  215.             if ( (part == kControlDownButtonPart) || (part == kControlPageDownPart) )
  216.                 amount = -amount;        /* reverse direction for a downer */
  217.             
  218.             amount = value - amount;
  219.             if ( amount < 0 )
  220.                 amount = 0;
  221.             else if ( amount > max )
  222.                 amount = max; 
  223.             SetControlValue(control, amount);
  224.             
  225.             amount = value - amount;        /* calculate the real change */
  226.             
  227.             if ( amount != 0 ) 
  228.             {
  229.                 if (control == (**doc).hScroll)
  230.                     hAmount = amount;
  231.                 else
  232.                     vAmount = amount ;
  233.                         
  234.                 MyScrollPicture(window,hAmount, vAmount);
  235.             }
  236.         }    
  237.     }    
  238. }
  239.     
  240.  
  241. //----------------------------------------------------------------------
  242. //
  243. //    MyScrollPicture -
  244. //                 
  245. //
  246. //----------------------------------------------------------------------
  247.  
  248. void MyScrollPicture(WindowRef window, short hs, short vs)
  249. {
  250.     RgnHandle        updateRgn;
  251.     GWorldPtr        oldWorld;
  252.     GWorldPtr        theWorld;
  253.     GDHandle        oldGD;
  254.     PixMapHandle    thePix;
  255.     Rect            contRect;
  256.     Rect            pictRect;
  257.     DocHnd            doc;
  258.     
  259.     doc = (DocHnd)GetWRefCon(window);
  260.     
  261.     if (doc != nil) 
  262.     {
  263.         GetGWorld(&oldWorld, &oldGD);
  264.         SetPort(window);
  265.         GetContRect(window, &contRect);
  266.         ScrollRect(&contRect, hs, vs, updateRgn = NewRgn());
  267.         
  268.         theWorld = (**doc).world;
  269.         pictRect = theWorld->portRect;
  270.         
  271.         thePix = GetGWorldPixMap(theWorld);
  272.         if (LockPixels(thePix)) 
  273.         {
  274.             SetGWorld((CGrafPtr)window, nil);
  275.             OffsetRect(&pictRect, -GetControlValue((**doc).hScroll), 
  276.                       -GetControlValue((**doc).vScroll));
  277.  
  278.             CopyBits((BitMap *) *thePix, &window->portBits,
  279.                     &theWorld->portRect, &pictRect, srcCopy,updateRgn);
  280.             
  281.             UnlockPixels(thePix);
  282.             
  283.             SetGWorld(oldWorld, oldGD);
  284.         }            
  285.         ClipRect(&window->portRect);
  286.         DisposeRgn(updateRgn);
  287.     }
  288.         
  289.  
  290. }    
  291.  
  292.  
  293. //----------------------------------------------------------------------
  294. //
  295. //    GetContRect -
  296. //                 
  297. //
  298. //----------------------------------------------------------------------
  299.  
  300. void GetContRect(WindowRef window,Rect *contRect)
  301. {
  302.     *contRect = window->portRect;            // get the content rect for our window
  303.     
  304.     contRect->right -= 15;                    // subtract the scroll bars from the rect
  305.     contRect->bottom -= 15;
  306.  
  307.  
  308. }
  309.